<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/09/2
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Driver;
use think\Db;
use think\Config;



/**
 * 汽车驾驶员🚚
 */
class Cardriver extends Driver
{

    protected $noNeedLogin = ['p','test_grab_order1'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
        // $user = $this->auth->getUserinfo();

        // if($user->group_id==1){
        //     $this->error('普通用户！非司机');
        // }
        
    }


    /**
     * 编辑用户信息(完善信息)
     *
     * @ApiSummary  ('ltd_name','sfz1_image','sfz2_image','drive_images','driving_images','name','sfz_number')
     * @return void
     */
    public function edit_user_data()
    {
        $params = $this->request->param();
        $field = ['ltd_name','sfz1_image','sfz2_image','drive_images','driving_images','name','sfz_number'];
        $data = $this->get_field_arr($params,$field);
        // halt($data);
        if(!$data){
            $this->error('没有修改任何信息');
        }
        $res = Db::name('user')->where('id',$this->auth->id)->update($data);
        $this->success('编辑成功',$res);
    }

    /**
     * （普通订单）获取待接单
     * @param string $s_city 始发城市
     * @param string $e_city 目的城市
     * @param string $load_status 装车类型:1=整车,2=零担
     * @param string $lay_status 装卸类型:1=一装一卸,2=一装两卸,3=两装一卸,4=两装两卸
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function get_order1_list($page=1,$limit=10,$s_city=null,$e_city=null,$load_status=null,$lay_status=null)
    {
        if($s_city){
            $s_ids = $this->GetAddrCityIds($s_city);
            if($s_ids){
                $where['s_addr_id'] = ['in',$s_ids];
            }
        }

        if($e_city){
            $e_ids = $this->GetAddrCityIds($e_city);
            if($e_ids){
                $where['e_addr_id|e2_addr_id'] = ['in',$e_ids];
            }
        }

        if($load_status){
            $where['load_status'] = ['=',$load_status];
        }

        if($lay_status){
            $where['lay_status'] = ['=',$lay_status];
        }

        // $where['j_uid'] = $this->auth->id;
        $where['spay_status'] = '1';//发货人支付押金:1=已支付,2=未支付,3=已违约
        $where['j_uid'] = NULL;

        if($this->IsVip()){ //如果是VIP正常获取数据 
            // $where['pay_time'] = ['<',time()]; //正常 
        }else{
            $where['pay_time'] = ['<',time()-Config::get('site.order_public_sec')]; //延后时间 
        }

        $list = Db::name('order1')
                    ->where($where)
                    ->order('time desc')
                    ->page($page,$limit)
                    ->select();
        if($list){
            foreach ($list as $key => &$val) {
                $val['pay_order'] = Db::name('pay_order')->where('uid',$this->auth->id)->where('order_id',$val['id'])->find();
                $val['s_addr'] = $this->GetAddrData($val['s_addr_id']);
                $val['e_addr'] = $this->GetAddrData($val['e_addr_id']);
                if($val['s2_addr_id']>0)
                    $val['s2_addr'] = $this->GetAddrData($val['s2_addr_id']);
                if($val['e2_addr_id']>0)
                    $val['e2_addr'] = $this->GetAddrData($val['e2_addr_id']);
                $val['user'] = $this->GetSuserData($val['s_uid']);
            }
        }
        $this->success('普通发货订单',$list);
    }



    /**
     * （普通订单）获取我的接单
     * @param string $s_city 始发城市
     * @param string $e_city 目的城市
     * @param string $load_status 装车类型:1=整车,2=零担
     * @param string $lay_status 装卸类型:1=一装一卸,2=一装两卸,3=两装一卸,4=两装两卸
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $status 状态:2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
     * @return void
     */
    public function get_my_order1_list($page=1,$limit=10,$s_city=null,$e_city=null,$load_status=null,$lay_status=null,$status=null)
    {
        $where['j_uid'] = $this->auth->id;
        if($status){
            $where['status'] = ['=',$status];
        }

        if($s_city){
            $s_ids = $this->GetAddrCityIds($s_city);
            if($s_ids){
                $where['s_addr_id'] = ['in',$s_ids];
            }
        }

        if($e_city){
            $e_ids = $this->GetAddrCityIds($e_city);
            if($e_ids){
                $where['e_addr_id|e2_addr_id'] = ['in',$e_ids];
            }
        }

        if($load_status){
            $where['load_status'] = ['=',$load_status];
        }

        if($lay_status){
            $where['lay_status'] = ['=',$lay_status];
        }

        // $where['spay_status'] = '1';//发货人支付押金:1=已支付,2=未支付,3=已违约
       


        $list = Db::name('order1')
                    ->where($where)
                    ->order('time desc')
                    ->page($page,$limit)
                    ->select();
        if($list){
            foreach ($list as $key => &$val) {
                $val['pay_order'] = Db::name('pay_order')->where('uid',$this->auth->id)->where('order_id',$val['id'])->find();
                $val['s_addr'] = $this->GetAddrData($val['s_addr_id']);
                $val['e_addr'] = $this->GetAddrData($val['e_addr_id']);
                if($val['s2_addr_id']>0)
                    $val['s2_addr'] = $this->GetAddrData($val['s2_addr_id']);
                if($val['e2_addr_id']>0)
                    $val['e2_addr'] = $this->GetAddrData($val['e2_addr_id']);
                $val['user'] = $this->GetSuserData($val['s_uid']);
            }
        }
        $this->success('普通发货订单',$list);
    }
    /**
     * 查看普通订单1信息
     *
     * @param int $order_id 订单id
     * @return void
     */
    public function look_order1_data($order_id)
    {
        $order = $this->DriverGetOrder1Data($order_id);
        $order['user'] = $this->GetSuserData($order['s_uid']);
        $this->success('ok',$order);
    }

    /**
     * 抢普通订单
     *
     * @param int $order_id 订单id
     * @return void
     */
    public function grab_order1($order_id)
    {
        $order = $this->DriverGetOrder1Data($order_id);
        if(!empty($order['j_uid'])){
            $this->error('已被接单了哦');
        }
        $data['j_uid'] = $this->auth->id;
        $data['j_time'] = time();
        $data['status'] = 2;
        Db::name('order1')->where('id',$order_id)->update($data);
        //推送给用户消息已被接单


        $this->success('ok',$order);
    }



    /**
     * 测试🧨抢普通订单
     *
     * @param int $order_id 订单id
     * @return void
     */
    public function test_grab_order1($order_id)
    {
        $s = microtime(true);
        $order = $this->DriverGetOrder1Data($order_id);
        $z1 = microtime(true);
        if(!empty($order['j_uid'])){
            $this->error('已被接单了哦');
        }
        $data['j_uid'] = null;//$this->auth->id;
        $data['j_time'] = null;
        Db::name('order1')->where('id',$order_id)->update($data);
        $z2=microtime(true);

        //推送给用户消息已被接单

        $s = '查询订单耗时:'.($z1-$s).'修改耗时'.($z2-$z1).'总耗时'.($z2-$s);
        $this->success($s,$order);
    }





    /**
     * （城市搬家订单）获取待接单
     * 
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $s_city 始发城市
     * @param string $e_city 目的城市
     * @param string $type_status 运输类型:1=运货,2=搬家
     * @return void
     */
    public function get_order2_list($page=1,$limit=10,$s_city=null,$e_city=null,$type_status=null)
    {

        if($type_status){
            $where['type_status'] = $type_status;
        }
        if($s_city){
            $s_ids = $this->GetAddrCityIds($s_city);
            if($s_ids){
                $where['s_addr_id'] = ['in',$s_ids];
            }
        }

        if($e_city){
            $e_ids = $this->GetAddrCityIds($e_city);
            if($e_ids){
                $where['e_addr_id'] = ['in',$e_ids];
            }
        }
        // $where['j_uid'] = $this->auth->id;
        $where['spay_status'] = '1';//发货人支付押金:1=已支付,2=未支付,3=已违约
        $where['j_uid'] = NULL;

        if($this->IsVip()){ //如果是VIP正常获取数据 
            // $where['pay_time'] = ['<',time()]; //正常 
        }else{
            $where['pay_time'] = ['<',time()-Config::get('site.order_public_sec')]; //延后时间 
        }

        $list = Db::name('order2')
                ->where($where)
                ->order('time desc')
                ->page($page,$limit)
                ->select();
        if($list){
            foreach ($list as $key => &$val) {
                $val = $this->DriverGetOrder2Data($val);
                // GetCarData
                // $val['pay_order'] = Db::name('pay_order')->where('uid',$this->auth->id)->where('order_id',$val['id'])->find();
                // $val['s_addr'] = $this->GetAddrData($val['s_addr_id']);
                // $val['e_addr'] = $this->GetAddrData($val['e_addr_id']);
                // $val['user'] = $this->GetSuserData($val['s_uid']);
                // $val['car'] = $this->GetCarData($val['car_id']);
                
            }
        }
        $this->success('城市搬家订单',$list);
    }

    /**
     * 查看城市搬家订单2信息
     *
     * @param int $order_id 订单id
     * @return void
     */
    public function look_order2_data($order_id)
    {
        $order = $this->DriverGetOrder2Data($order_id);
        $order['user'] = $this->GetSuserData($order['s_uid']);
        $this->success('ok',$order);
    }

    /**
     * 抢城市搬家订单
     *
     * @param int $order_id 订单id
     * @return void
     */
    public function grab_order2($order_id)
    {
        $order = $this->DriverGetOrder2Data($order_id);
        if(!empty($order['j_uid'])){
            $this->error('已被接单了哦');
        }

        $data['j_uid'] = $this->auth->id;
        $data['j_time'] = time();
        $data['status'] = 2;
        Db::name('order2')->where('id',$order_id)->update($data);
        //推送给用户消息已被接单

        
        $this->success('ok',$order);
    }


    /**
     * （城市搬家）获取我的接单
     * @param string $s_city 始发城市
     * @param string $e_city 目的城市
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $status 状态:2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
     * @param string $type_status 运输类型:1=运货,2=搬家
     * @return void
     */
    public function get_my_order2_list($page=1,$limit=10,$s_city=null,$e_city=null,$status=null,$type_status= null)
    {
        $where['j_uid'] = $this->auth->id;
        if($status){
            $where['status'] = ['=',$status];
        }

        if($s_city){
            $s_ids = $this->GetAddrCityIds($s_city);
            if($s_ids){
                $where['s_addr_id'] = ['in',$s_ids];
            }
        }

        if($e_city){
            $e_ids = $this->GetAddrCityIds($e_city);
            if($e_ids){
                $where['e_addr_id'] = ['in',$e_ids];
            }
        }

        // if($load_status){
        //     $where['load_status'] = ['=',$load_status];
        // }

        if($type_status){
            $where['type_status'] = ['=',$type_status];
        }

        // $where['spay_status'] = '1';//发货人支付押金:1=已支付,2=未支付,3=已违约


        $list = Db::name('order2')
                    ->where($where)
                    ->order('time desc')
                    ->page($page,$limit)
                    ->select();
        if($list){
            foreach ($list as $key => &$val) {
                // $val['pay_order'] = Db::name('pay_order')->where('uid',$this->auth->id)->where('order_id',$val['id'])->find();
                // $val['s_addr'] = $this->GetAddrData($val['s_addr_id']);
                // $val['e_addr'] = $this->GetAddrData($val['e_addr_id']);
      
                // $val['user'] = $this->GetSuserData($val['s_uid']);
                // $val['car'] = $this->GetCarData($val['car_id']);
                $val = $this->DriverGetOrder2Data($val);
            }
        }
        $this->success('城市搬家',$list);
    }



     /**
     * 司机发布回程顺风车📦
     *
     * @param int $s_addr_id 装货地址
     * @param int $s2_addr_id 装货地址2
     * @param int $e_addr_id 卸货地址
     * @param int $e2_addr_id 卸货地址2  
     * @param int $s_load_time 发车开始时间
     * @param int $e_load_time 最晚出发时间            
     * @param string $s_ps 发货人备注信息            
     * @param int $car2_id 车2id
     * @param decimal $weight 吨
     * @param decimal $long 长
     * @param decimal $wide 宽
     * @param decimal $high 高
     * @param string $tag_ids 标签ids
     * @param string $car_number 车牌号
     * @return void
     */
    
    public function add_order_return(  $s_addr_id = null,
            $s2_addr_id = null,
            $e_addr_id = null,
            $e2_addr_id = null,
            $s_load_time = null,
            $e_load_time = null,
            $s_ps = null,

            // $goods_name = null,
            $long = null,
            $wide = null,
            $high = null,$weight = null,$tag_ids=null,$often_status=0,$km=null,$car2_id,$car_number=null){
 



        $data['s_addr_id'] = $s_addr_id;
        $data['s2_addr_id'] = $s2_addr_id;
        $data['e_addr_id'] = $e_addr_id;
        $data['e2_addr_id'] = $e2_addr_id;
        $data['s_load_time'] = strtotime($s_load_time);
        $data['e_load_time'] = strtotime($e_load_time);
        $data['s_ps'] = $s_ps;
        // $data['load_status'] = $load_status;
        $data['car_number'] = $car_number;
        $data['long'] = $long;
        $data['wide'] = $wide;
        $data['high'] = $high;
        $data['weight'] = $weight;
        $data['tag_ids'] = $tag_ids;
        $data['often_status'] = $often_status;
        $data['car2_id'] = $car2_id;
        $data['km'] = $km;



        $data['s_money'] = Config::get('site.s_money1');
        $data['j_money'] = Config::get('site.j_money1');

        //     $data = $this->get_field_arr($params,$fields,true);
            $data['j_uid'] = $this->auth->id;
            
            $data['status'] = '1';//'状态:1=待接单,2=已接单,3=已完成,9=已取消',
            $data['lay_status'] = '1';//装卸类型:1=一装一卸,2=一装两卸,3=两装一卸,4=两装两卸',

            
            $this->AddAddrUseNum($data['s_addr_id']);
            $this->AddAddrUseNum($data['e_addr_id']);
            if(!empty($data['s2_addr_id'])){
                $this->AddAddrUseNum($data['s2_addr_id']);
                if(!empty($data['e2_addr_id'])){
                    $data['lay_status'] = '4';
                    $this->AddAddrUseNum($data['e2_addr_id']);
                }else{
                    $data['lay_status'] = '3';
                }
            }elseif(!empty($data['e2_addr_id'])){
                $data['lay_status'] = '2';
                $this->AddAddrUseNum($data['e2_addr_id']);
            }
            $data['order'] = $this->rand_order('OrderR',$this->auth->id);


            $data['size'] = $long * $wide * $weight;

            $data['time'] = time();
            $data['id'] = Db::name('order_return')->insertGetId($data);

            $this->success('请选择支付方式',$data);

            //支付订单
            // $pay_data = $this->PayOrder($data['id'],1,$this->auth->id,'3');
            // $this->success('pay',$pay_data);
   
    }


    /**
    * 获取我的回程订单列表
    *
    * @param integer $page 1
    * @param integer $limit 5
    * @param integer $status 状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
    * @return void
    */
    public function get_order_return_list($page=1,$limit=5,$status=null)
    {
        $where['j_uid'] = ['=',$this->auth->id];
        if($status){
            $where['status'] = ['=',$status];
        }
        $list = Db::name('order_return')->where($where)->page($page,$limit)->select();
        if($list){
            foreach ($list as $key => &$val) {
                $val = $this->GetOrder4FieldData($val);      
                if($val['s_uid']){
                    $val['user'] = $this->GetSuserData($val['s_uid']);
                    // $val['user'] = Db::name('user')->where('id',$val['s_uid'])->find();
                }           
            }
        }
        $this->success('ok',$list);
    }
    

    /**
     * 获取回程单信息
     * 
     * @param int $id 订单id
     * @return void
     */ 
    public function get_order_return_data($id)
    {
        $data = $this->GetOrder4Data($id);
        if($data['j_uid']!=$this->auth->id){
            $this->error('非自己发布的订单不能查看');
        }

        $this->success('ok',$data);
    }

    /**
     * 司机支付通用押金订单
     * @param string $order_id 订单id（2选1）
     * @param string $order 订单号（2选1）
     * @param string $order_status 1=普通发货,2=城市快运,3=拉货搬家,4=顺风车
     * @param string $pay_type 默认3支付类型:1=微信,2=支付宝,3=钱包
     * @return void
     */
    public function j_pay_order($order_id=null,$order=null,$order_status,$pay_type = 3)
    {
        if(!$order_id){
           
            $order_data = Db::name($this->GetTableByOrderStatus($order_status))->where('order',$order)->find();
            if(!$order_data){
                $this->error('没有该订单');
            }
            $order_id = $order_data['id'];
        }
        // * @param string $order_id 订单id
        // * @param string $order_status 1=普通发货,2=城市快运,3=拉货搬家,4=顺风车
        // * @param string $uid 用户id
        // * @param string $role_status 角色:1=货主,2=司机
        // * @param string $pay_type 支付类型:1=微信,2=支付宝,3=钱包
        $res = $this->PayOrder($order_id,$order_status,$this->auth->id,'2',$pay_type);
        $this->success('pay',$res);
    }

    /**
     * 司机查询通用押金订单
     * @param string $order_id 订单id（2选1）
     * @param string $order 订单号（2选1）
     * @param string $order_status 1=普通发货,2=城市快运,3=拉货搬家,4=顺风车
     * @return void
     */
    public function j_query_order($order_id=null,$order=null,$order_status,$pay_type = 3)
    {
        if(!$order_id){
            $order_data = Db::name($this->GetTableByOrderStatus($order_status))->where('order',$order)->find();
            if(!$order_data){
                $this->error('没有该订单');
            }
            $order_id = $order_data['id'];
        }

        $where['order_id']= $order_id;
        $where['role_status'] = '2';
        $where['uid'] = $this->auth->id;
        $res = Db::name('pay_order')->where($where)->find();
        $this->success('pay',$res);
    }

    

    /**
     * 司机更新订单状态
     * @param string $order_status 1=普通发货,2=城市快运,3=拉货搬家,4=顺风车
     * @param int $id 订单号id
     * @param int $status 状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
     * @return void
     */
    public function update_order_status($id,$status,$order_status)
    {
        $where['j_uid'] = $this->auth->id;
        $where['id'] = $id;
        $db = $this->GetTableByOrderStatus($order_status);
        $res = Db::name($db)
                ->where($where)
                ->find();
        if(!$res){
            $this->error('没有该订单');
        }
        switch ($status) {
            case '1':
                // if($res['status']!='9')
                // $this->error('当前订单待接单');
                break;

            case '2':
                // if($res['status']!='9')
                $this->error('当前已接单,不能修改装');
                break;

            case '3':
                if($res['status']!='2')
                $this->error('未接单');
                
                break;

            case '4':         
                if($res['status']!='3')      
                $this->error('司机未到达目的地');
                
                break;

            case '5':
                if($res['status']!='4')
                $this->error('货主未确认已装车');
                
                break;

            case '6':     
                if($res['status']!='5')
                $this->error('司机未确认送达');
                break;
        //1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
            default:
                $this->error('暂未加入此状态');
                break;
        }
        $this->UpdateOrderStatus($id,$status,$db);

        $this->success('修改成功');
    }

    /**
     * 修改订单状态
     *
     * @param int $id 订单id
     * @param int $status 状态
     * 
     * @return void
     */
    protected function UpdateOrderStatus($id,$status,$db){

        //后期加入ws推送给司机
        Db::name($db)->where('id',$id)->update(['status'=>$status]);
    }

}